class Solution {
public:
    void sortColors(vector<int>& nums) {
        int key = 0, left = -1, right = nums.size();
        while(key < right)
        {
            if(nums[key] == 0)
                swap(nums[++left], nums[key++]);
            else if(nums[key] == 1)
                key++;
            else
                swap(nums[--right], nums[key]);
        }
    }
};